package top.lshaci.learning.alicloud.oss.properties;

import com.aliyun.oss.crypto.SimpleRSAEncryptionMaterials;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

/**
 * OssEncryptionProperties
 *
 * @author lshaci
 * @since 1.0.0
 */
@Data
@Validated
@ConfigurationProperties("oss.encryption")
public class OssEncryptionProperties {

    /**
     * 是否开启oss加密客户端配置
     **/
    private Boolean enabled;

    /**
     * 加密用的公钥
     */
    @NotBlank
    private String publicKey;

    /**
     * 加密用的私钥
     */
    @NotBlank
    private String privateKey;

    /**
     * 根据公钥和私钥生成密钥对
     *
     * @return 密钥对
     */
    public KeyPair keyPair() {
        RSAPrivateKey privateKey = SimpleRSAEncryptionMaterials.getPrivateKeyFromPemPKCS1(this.privateKey);
        RSAPublicKey publicKey = SimpleRSAEncryptionMaterials.getPublicKeyFromPemX509(this.publicKey);
        return new KeyPair(publicKey, privateKey);
    }
}
